package com.whfc.ms.api.safety.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.safety.service.MsSafetyFineService;
import com.whfc.ms.config.AbstractBaseController;
import com.whfc.safety.dto.SafetyFineDTO;
import com.whfc.safety.dto.SafetyFineNumDTO;
import com.whfc.safety.param.SafetyFineAddParam;
import com.whfc.safety.param.SafetyFineCompleteParam;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description 安全罚款单请求类
 * @Author hw
 * @Date 2021/8/13
 * @Version 1.0
 */
@RequiresPermissions("safety/fine")
@RestController
@RequestMapping("/ms/api/safety/fine")
public class SafetyFineController extends AbstractBaseController {


    @Autowired
    private MsSafetyFineService msSafetyFineService;


    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "corpName", required = false) String corpName,
                       @RequestParam(value = "userName", required = false) String userName,
                       @RequestParam(value = "state", required = false) String state,
                       @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                       @RequestParam(value = "pageSize") Integer pageSize,
                       @RequestParam(value = "pageNum") Integer pageNum) {
        logger.info("质量罚款单请求列表,loginer:{},deptId：{},corpName：{},userName：{},state：{},startTime：{},endTime：{},pageNum：{},pageSize：{}",
                user.getUsername(), deptId, corpName, userName, state, startTime, endTime, pageNum, pageSize);
        PageData<SafetyFineDTO> list = msSafetyFineService.list(deptId, corpName, userName, state, startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @GetMapping("/num")
    public Result num(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestParam("deptId") Integer deptId,
                      @RequestParam(value = "corpName", required = false) String corpName,
                      @RequestParam(value = "userName", required = false) String userName,
                      @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                      @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("质量罚款单请求统计,loginer:{},deptId：{},corpName：{},userName：{},startTime：{},endTime：{}",
                user.getUsername(), deptId, corpName, userName, startTime, endTime);
        SafetyFineNumDTO data = msSafetyFineService.num(deptId, corpName, userName, startTime, endTime);
        return ResultUtil.success(data);
    }

    @RequiresPermissions("safety/fine/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody @Validated SafetyFineAddParam param) {
        logger.info("新增质量罚款单请求,loginer:{},param:{}", user.getUsername(), param);
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        msSafetyFineService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("safety/fine/del")
    @PostMapping("/del/{fineId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("fineId") Integer fineId) {
        logger.info("删除质量罚款单请求,loginer:{},fineId:{}", user.getUsername(), fineId);
        msSafetyFineService.del(fineId);
        return ResultUtil.success();
    }

    @RequiresPermissions(value = {"safety/fine/approval", "safety/fine/check"}, logical = Logical.OR)
    @PostMapping("/complete")
    public Result complete(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestBody @Validated SafetyFineCompleteParam param) {
        logger.info("审批/批准罚款单,loginer:{},param:{}", user.getUsername(), param);
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        msSafetyFineService.complete(param);
        return ResultUtil.success();
    }

    @GetMapping("/export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("deptId") Integer deptId,
                         @RequestParam("fineIds") String fineIds) {
        logger.info("导出罚款单,loginer:{},deptId:{},fineIds:{}", user.getUsername(), deptId, fineIds);
        msSafetyFineService.export(fineIds, deptId);
        return ResultUtil.success();
    }

    /**
     * 2.0安全罚款单批量删除
     *
     * @param idList
     * @return
     */
    @RequiresPermissions("safety/fine/del")
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestBody List<Integer> idList) {
        logger.info("批量删除安全罚款单请求,userId:{},idList:{}", getLoginUser().getId(), idList);
        AssertUtil.isTrue(CollectionUtils.isEmpty(idList), "ID不能为空");
        msSafetyFineService.deleteBatch(idList);
        return R.success();
    }

}
